package com.jky.stmall.manger.interceptor;

import com.jky.stmall.manger.entity.Admin;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Component
public class AdminInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //执行拦截器之前执行的
        //验证登陆用户
        HttpSession session= request.getSession();
        Admin admin=(Admin) session.getAttribute("admin");
        if (admin==null){
            //跳转到登陆页面
            response.sendRedirect("/admin/login.html");
        }
        //放行返回true ，拦截返回false
        return true;
    }
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    //处理器处理完成时执行 数据还在handleradapter
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        //所有处理都完成时执行 视图解析器也完成
    }
}
